<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<?php require VIEW_ROOT.'/common/header.php'?>

<script>
var not_id = <?php echo empty($res['not_id']) ? '0' : $res['not_id']?>;
var content = '';
var content_now = '';	//输入框中的实际内容

$(function() {
    document.getElementById('content').focus();
	save();
});

function save() {
	content_now = $.trim($('#content').val());

	if(content_now !== content && not_id === 0) {
		content = content_now;

        $('#status').html('正在创建并保存...');
        $('#status').addClass('saving');

		// 第一次保存
		$.post('./?m=note&a=add_ajax', {cnt : content}, function(data) {
			not_id = parseInt(data);
			$('#status').html('已创建并保存');
            $('#time').html(get_time());
            $('#status').removeClass('saving');
			show_delete_button();
			setTimeout('save()', 500);
		});
	} else if(content_now !== content && not_id != 0) {
		content = content_now;

        $('#status').html('正在保存...');
        $('#status').addClass('saving');

		// 第二次及以后保存
		$.post('./?m=note&a=add_ajax', {cnt : content, id : not_id}, function(data) {
			$('#status').html('已保存');
            $('#status').removeClass('saving');
            $('#time').html(get_time());
            $('#return').html('...' + data);
			setTimeout('save()', 500);
		});
	} else {
		setTimeout('save()', 500);
	}
}

function get_time() {
	d = new Date();
	return d.toLocaleTimeString();
}

function show_delete_button() {
	$('#delete_button').show();
}

function delete_ajax() {
    $('#status').html('正在删除...');
    $('#status').addClass('saving');

	$.get('./?m=note&a=delete_ajax', {id : not_id}, function() {
		// 删除成功后的初始化
		not_id = 0;
		$('#content').val('');
		content = '';
		content_now = '';
		$('#content').focus();
        $('#status').removeClass('saving');
		$('#status').html('删除成功');
        $('#time').html(get_time());
        $('#return').html('');
		$('#delete_button').hide();
	});
}

function height_add(num) {
	var obj = $('#content');
	var height_now = parseInt(obj.css('height'));
	var height_then = height_now + num;
	
	(height_then < 100) && (height_then = 100);
	
	obj.css('height', height_then + 'px');
}
</script>

<style>
.status { background:#68af02; color:#fff; padding:1px 10px; border-radius:3px; }
.saving { background:#EF8F00; }
.time { padding:0 10px; font-size:12px; }
.return { color:#999; font-size:12px; }
</style>

<div class="info">
	<span id="status" class="status">请输入日志内容</span>
	<span id="time" class="time"></span>
	<span id="return" class="return"></span>
</div>
<table>
	<tr>
		<th>日志内容</th>
	</tr>
	<tr>
		<td class="frm">
			<textarea name="content" id="content" style="width:100%; height:400px;"><?php echo empty($res['content']) ? '' : $res['content']?></textarea>
		</td>
	</tr>
	<tr>
		<td class="frm">
			<button onclick="height_add(100)">加高(+)</button>
			<button onclick="height_add(-100)">减高(-)</button>
			<button onclick="delete_ajax()" id="delete_button" style="color:red;display:none;">删除</button>
		</td>
	</tr>
</table>

<?php require VIEW_ROOT.'/common/footer.php'?>
